//闹钟触发及闹铃播放相关


#include <Wire.h>
#include <RTClib.h>
#include <U8g2_for_Adafruit_GFX.h>  //甘草修改的u8g2图像库
#include <GxEPD2_BW.h>
#include <Adafruit_GFX.h>  //图像库
#include <string>
#include <esp_sleep.h>
#include <_Time.h>
DateTime alarm_l;
const byte rtc_int = 4;

int clockflag = 0;
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
esp_sleep_wakeup_cause_t wakeup_reason;
bool isAlarmTriggered(void) {
  uint8_t data;
  bool flag;

  Wire.beginTransmission(0x51);
  Wire.write(0x01);
  Wire.endTransmission();

  Wire.requestFrom(0x51, 1);
  data = Wire.read();
  flag = bitRead(data, 3);

  return (flag);
}


float Soundscale[8]{
  0, 261.6, 293.7, 329.6, 349.2, 392, 440, 493.9
};
float soundchoice(char x) {
  switch (x) {
    case '0': return Soundscale[0]; break;

    case 'a': return Soundscale[1]; break;
    case 'b': return Soundscale[2]; break;
    case 'c': return Soundscale[3]; break;
    case 'd': return Soundscale[4]; break;
    case 'e': return Soundscale[5]; break;
    case 'f': return Soundscale[6]; break;
    case 'g': return Soundscale[7]; break;

    case '1': return 2 * Soundscale[1]; break;
    case '2': return 2 * Soundscale[2]; break;
    case '3': return 2 * Soundscale[3]; break;
    case '4': return 2 * Soundscale[4]; break;
    case '5': return 2 * Soundscale[5]; break;
    case '6': return 2 * Soundscale[6]; break;
    case '7': return 2 * Soundscale[7]; break;

    case 'A': return 4 * Soundscale[1]; break;
    case 'B': return 4 * Soundscale[2]; break;
    case 'C': return 4 * Soundscale[3]; break;
    case 'D': return 4 * Soundscale[4]; break;
    case 'E': return 4 * Soundscale[5]; break;
    case 'F': return 4 * Soundscale[6]; break;
    case 'G': return 4 * Soundscale[7]; break;
  }
}
//两个音频代码
String music0 = "3s2356s5As60s535L60sABCBsAs6A0s5L05s35L60sAlBsCAl650s523s5321sf103s212l30s5l6A650s5323s5320s1s2f0102l3121f0s1fe003s212l30s56A650s5323s53s201a2fL10sBlCABA60s56ACBA6A0s5L";
String changtingwai="5l3s5AL0s6As65L0s5l1s23l2s12L005l3s5Al70s6lAl5L05l2s34lg1L006lAlAL07l6s7AL06s7As66s53s10s2L0s53s5Al70s6lAl5l0s5l2s34lg0s1L005L2s34Lg0s1L0000";
void alarmmusic(String music) {//音频播放
  pinMode(0, OUTPUT);
  int musictick = 0;
  int leavetick = 0;
  float scalet = 1;
  char x0 = 'a';
  char x1 = 'a';
  while (1) {
    if (musictick != music.length() - 1) {
      x0 = music.charAt(musictick);
      x1 = music.charAt(musictick + 1);
      tone(0, soundchoice(x0));
    } else {
      x0 = music.charAt(musictick);
      x1 = 'o';
    }

    switch (x1) {
      case 's': scalet = 0.75; break;
      case 'S': scalet = 0.5; break;
      case 'l': scalet = 1.5; break;
      case 'L': scalet = 2; break;
      default: scalet = 1; break;
    }
    delay(350 * scalet);
    if (tapcheck(1) || tapcheck(2) || tapcheck(3)) leavetick++;
    if (leavetick == 2) break;
    switch (x1) {
      case 's': musictick += 2; break;
      case 'S': musictick += 2; break;
      case 'l': musictick += 2; break;
      case 'L': musictick += 2; break;
      case 'o': musictick = 0;delay(1000); break;
      default: musictick = (musictick < music.length() - 1) ? musictick + 1 : 0; break;
    }
  }


  pinMode(0, INPUT);
}
void isAlarmedcheck() {
  // SPI.end();
  // Wire.begin(8, 7);
  if (isAlarmTriggered()) {
    //Wire.begin(8, 7);
    Serial.println("off_alarm");
    RTC.off_alarm();
    //setAlarm(80, 80);
    Wire.end();
    //delay(200);
    SPI.begin(7, 8, 8, 6);
    EEPROM.write(6, 100);
    EEPROM.commit();
    alTime.ok=false;
    display.fillScreen(GxEPD_BLACK);
    display.setTextColor(GxEPD_WHITE);
    u8g2Fonts.setFont(chinese_gb2312);
    u8g2Fonts.setForegroundColor(GxEPD_WHITE);
    u8g2Fonts.setBackgroundColor(GxEPD_BLACK);
    centerx("闹钟已触发", 60, 1, 1, 98);
    display.nextPage();
    u8g2Fonts.setForegroundColor(FormC);
    u8g2Fonts.setBackgroundColor(BackC);
    //
if(alTime.ok){
  alarmmusic(changtingwai);
}
    
    SPI.end();
    Wire.begin(8, 7);
    Serial.println("WakeUp atmega");
    clockflag = 0;
  } else {
    // Wire.end();
    // SPI.begin(7, 8, 8, 6);
  }
}
void alarmget() {
  SPI.end();
  Wire.begin(8, 7);
  DateTime alarmgot = RTC.get_alarm();
  Wire.end();
  SPI.begin(7, 8, 8, 6);
  u8g2Fonts.setForegroundColor(BackC);  //GxEPD_BLACK            // 设置前景色
  u8g2Fonts.setBackgroundColor(FormC);  //GxEPD_WHITE      // 设置背景色
  u8g2Fonts.setFont(chinese_gb2312);
  display.fillScreen(FormC);
  display.setTextColor(BackC);
  int H = alarmgot.hour();
  int M = alarmgot.minute();
  u8g2Fonts.drawUTF8(20, 20, (std::__cxx11::to_string(H)).c_str());
  u8g2Fonts.drawUTF8(20, 40, (std::__cxx11::to_string(M)).c_str());
  u8g2Fonts.drawUTF8(20, 60, "rtc");
  display.nextPage();
  delay(1000);
}
void setAlarm(int alarmmin, int alarmhour) {
  DateTime alarm = RTC.now();
  alarm.setminute(alarmmin);
  alarm.sethour(alarmhour);
  RTC.set_alarm(alarm, { 1, 1, 0, 0 });
  RTC.on_alarm();
}
